<div class="m-bg">
<h1>SQL WHERE 子句</h1>
<p class="intro">WHERE 子句用于过滤记录。</p> <h2>SQL WHERE 子句</h2> <p>WHERE 子句用于提取那些满足指定标准的记录。</p> <h3>SQL WHERE 语法</h3> <div class="code notranslate"><pre><code class="language-sql"><div> SELECT <em>column_name</em>,<em>column_name</em><br/> FROM <em>table_name</em><br/> WHERE <em>column_name operator value</em>;</div></code></pre></div> <h2>演示数据库</h2> <p>在本教程中，我们将使用众所周知的 Northwind 样本数据库。</p> <p>下面是选自 "Customers" 表的数据：</p> <table class="reference notranslate">
<tr>
<th>CustomerID</th> <th>CustomerName</th> <th>ContactName</th> <th>Address</th> <th>City</th> <th>PostalCode</th> <th>Country</th> </tr>
<tr>
<td>1<br/><br/>
</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Obere Str. 57</td> <td>Berlin</td> <td>12209</td> <td>Germany</td> </tr>
<tr>
<td>2</td> <td>Ana Trujillo Emparedados y helados</td> <td>Ana Trujillo</td> <td>Avda. de la Constitución 2222</td> <td>México D.F.</td> <td>05021</td> <td>Mexico</td> </tr>
<tr>
<td>3</td> <td>Antonio Moreno Taquería</td> <td>Antonio Moreno</td> <td>Mataderos 2312</td> <td>México D.F.</td> <td>05023</td> <td>Mexico</td> </tr>
<tr>
<td>4<br/><br/>
</td> <td>Around the Horn</td> <td>Thomas Hardy</td> <td>120 Hanover Sq.</td> <td>London</td> <td>WA1 1DP</td> <td>UK</td> </tr>
<tr>
<td>5</td> <td>Berglunds snabbköp</td> <td>Christina Berglund</td> <td>Berguvsvägen 8</td> <td>Luleå</td> <td>S-958 22</td> <td>Sweden</td> </tr>
</table>
<h2>WHERE 子句实例</h2> <p>下面的 SQL 语句从 "Customers" 表中选取国家为 "Mexico" 的所有客户：</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE Country='Mexico';</div></code></pre> </div> <h2>文本字段 vs. 数值字段</h2> <p>SQL 使用单引号来环绕文本值（大部分数据库系统也接受双引号）。</p> <p>如果是数值字段，请不要使用引号。</p> <div class="example margin-b-10"> <h2 class="example">实例</h2> <pre><code class="language-sql"><div class="example_code notranslate"> SELECT * FROM Customers<br/> WHERE CustomerID=1;</div></code></pre> </div> <h2>WHERE 子句中的运算符</h2> <p>下面的运算符可以在 WHERE 子句中使用：</p> <table class="reference notranslate">
<tr>
<th width="20%">运算符</th> <th width="80%">描述</th> </tr>
<tr>
<td>=</td> <td>等于</td> </tr>
<tr>
<td>&lt;&gt;</td> <td>不等于。<b>注释：</b>在 SQL 的一些版本中，该操作符可被写成 !=</td> </tr>
<tr>
<td>&gt;</td> <td>大于</td> </tr>
<tr>
<td>&lt;</td> <td>小于</td> </tr>
<tr>
<td>&gt;=</td> <td>大于等于</td> </tr>
<tr>
<td>&lt;=</td> <td>小于等于</td> </tr>
<tr>
<td>BETWEEN</td> <td>在某个范围内</td> </tr>
<tr>
<td>LIKE</td> <td>搜索某种模式</td> </tr>
<tr>
<td>IN</td> <td>指定针对某个列的多个可能值</td> </tr>
</table>
<div class="text-center padding-10 margin-t-5">
</div>
</div>